package com.tqjc.plccontrol.service.impl;

/**
 * @projectName: BaotouSteel
 * @className: ConnectInfoServiceImpl
 * @author: Patienyon
 * @description: 采集服务信息服务实现类
 * @date: 2023/6/13 10:20
 */

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tqjc.plccontrol.entity.ConnectInfo;
import com.tqjc.plccontrol.mapper.ConnectInfoMapper;
import com.tqjc.plccontrol.service.ConnectInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@DS("baotou")
public class ConnectInfoServiceImpl extends ServiceImpl<ConnectInfoMapper,ConnectInfo> implements ConnectInfoService {
    @Resource
    private ConnectInfoMapper connectInfoMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param sn 主键
     * @return 实例对象
     */
    public ConnectInfo queryById(String sn){
        return connectInfoMapper.selectById(sn);
    }

    /**
     * 分页查询
     *
     * @param connectInfo 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    public Page<ConnectInfo> paginQuery(ConnectInfo connectInfo, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<ConnectInfo> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(connectInfo.getName())){
            queryWrapper.eq(ConnectInfo::getName, connectInfo.getName());
        }
        if(StrUtil.isNotBlank(connectInfo.getPlcSn())){
            queryWrapper.eq(ConnectInfo::getPlcSn, connectInfo.getPlcSn());
        }
        //2. 执行分页查询
        Page<ConnectInfo> pagin = new Page<>(current , size , true);
        IPage<ConnectInfo> selectResult = connectInfoMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param connectInfo 实例对象
     * @return 实例对象
     */
    public ConnectInfo insert(ConnectInfo connectInfo){
        connectInfoMapper.insert(connectInfo);
        return connectInfo;
    }

    /**
     * 更新数据
     *
     * @param connectInfo 实例对象
     * @return 实例对象
     */
    public ConnectInfo update(ConnectInfo connectInfo){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<ConnectInfo> chainWrapper = new LambdaUpdateChainWrapper<ConnectInfo>(connectInfoMapper);
        if(StrUtil.isNotBlank(connectInfo.getName())){
            chainWrapper.eq(ConnectInfo::getName, connectInfo.getName());
        }
        if(StrUtil.isNotBlank(connectInfo.getPlcSn())){
            chainWrapper.eq(ConnectInfo::getPlcSn, connectInfo.getPlcSn());
        }
        //2. 设置主键，并更新
        chainWrapper.set(ConnectInfo::getSn, connectInfo.getSn());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(connectInfo.getSn());
        }else{
            return connectInfo;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param sn 主键
     * @return 是否成功
     */
    public boolean deleteById(String sn){
        int total = connectInfoMapper.deleteById(sn);
        return total > 0;
    }
}
